package com.wpf.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.entity.Equipment;
import com.wpf.system.entity.vo.EquipmentVO;
import com.wpf.system.service.EquipmentService;
import com.wpf.system.mapper.EquipmentMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author ferna
* @description 针对表【equipment(器械表)】的数据库操作Service实现
* @createDate 2023-01-19 13:17:05
*/
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService{

    @Override
    public IPage<Equipment> getEquipment(int page, int pageSize, Long cid, String searchContent) {

        IPage<Equipment> pageCondition = new Page<>(page, pageSize);

        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Equipment::getNumber);

        if(ObjectUtil.isNotNull(cid) && StrUtil.isNotBlank(searchContent)){
            // 根据类别和名称查询
            if(cid==0L){
                queryWrapper.isNull(Equipment::getCid);
            }else {
                queryWrapper.eq(Equipment::getCid,cid);
            }
            queryWrapper.like(Equipment::getName,searchContent);
        }else if(ObjectUtil.isNotNull(cid) && StrUtil.isBlank(searchContent)){
            //根据类别查询
            if(cid==0L){
                queryWrapper.isNull(Equipment::getCid);
            }else {
                queryWrapper.eq(Equipment::getCid,cid);
            }
        }else if(ObjectUtil.isNull(cid) && StrUtil.isNotBlank(searchContent)){
            //根据名称查询
            queryWrapper.like(Equipment::getName,searchContent);
        }

        return this.baseMapper.selectPage(pageCondition,queryWrapper);
    }

    @Override
    public int editEquipmentStatusById(Long id) {
        return this.baseMapper.editEquipmentStatusById(id);
    }

    @Override
    public List<EquipmentVO> getValidEquipment(Long cid, String name, Integer page, Integer pageSize) {
        return this.baseMapper.getValidEquipment(cid,name,page,pageSize);
    }
}




